package com.ymatou.iapi.hunter.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.hunter.paramater.OnOrderPayEventRequest;
import com.ymatou.iapi.hunter.service.OnOrderPayEventCall;
import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceHunterOrderCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;

public class Ts_OnOrderPayEvent {
	
	private static OnOrderPayEventRequest onOrderPayEventRequest;
	private static OnOrderPayEventCall onOrderPayEventCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("绑定用户关系");
	}

	@Before
	public void caseUp() {
		onOrderPayEventRequest = new OnOrderPayEventRequest();
		onOrderPayEventCall = new OnOrderPayEventCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(onOrderPayEventCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_OnOrderPayEvent_001() {
		Logger.start(true, "永久关系绑定成功");
		try {
			onOrderPayEventRequest.setHunterOrderType(1);;
			onOrderPayEventRequest.setHunterId(4321L);
			onOrderPayEventRequest.setOrderId(1889158622L);
			onOrderPayEventRequest.setBuyerId(100001L);;
			onOrderPayEventRequest.setUserLoginName("fans001");

			onOrderPayEventCall.setData(onOrderPayEventRequest);
			onOrderPayEventCall.callService();
			
			Logger.verifyEquals("ok", onOrderPayEventCall.getCode(),
					"验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_OnOrderPayEvent_002() {
		Logger.start(true, "分销普通商品订单下单成功,验证同步Mongo,ES数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			List<OrderPo> orderList = orderShardingWapper
					.selectHunterGiftBagOrderbyUserId(20238699);

			if (orderList.size() > 0) {
				for (OrderPo po : orderList) {
					TradingCallService.cancelOrder(po.getOrderId(),
							CancelOrderTypeEnum.BUYER_CANCEL_ORDER, 20238699);
				}
			}
			
			long hunterId = 1234;
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, 1);
			Long userId = placeOrder.getUserId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long tradingId = placeOrder.getTradingId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();
			
			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");
			
			PayGateWayCallService.notifyCall("13",
					thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue());
			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);
			Logger.comment("前置数据准备结束");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
